package com.hrt.web.comments.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.Comments;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.HRTMember;
import com.hrt.manage.member.service.HRTMemberService;
import com.hrt.util.PageUtils;
import com.hrt.web.comments.service.CommentsWebService;
import com.hrt.web.product.service.GiftDetailService;

@Controller
@RequestMapping("HRTWEB/commentPage")
public class CommentsWebController extends BaseController<Comments>{
	
	//评论
	@Autowired
	private CommentsWebService commentsWebService;
	
	//礼品规格
	@Autowired
	private GiftDetailService giftDetailService;
	
	//会员
	@Autowired
	private HRTMemberService hRTMemberService;
	
	
	
	/**查看某礼品追评分页*/
	@RequestMapping(value = "/getAddCommentByGdeId")
	@ResponseBody
	public Map<String, PageUtils<Comments>> getAddCommentByGdeId(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "ids", required = true) String gdeIds,
			HttpServletRequest request) {
		Map<String, PageUtils<Comments>> resultMap = new HashMap<String, PageUtils<Comments>>();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);
		setPageSize(pageSize);
		List<Comments> resultList = new ArrayList<Comments>();
		
		if (gdeIds == null) {
			throw new RuntimeException("礼品规格id有误");
		} else {
			
			List<Long> gdeIdsList = commentsWebService.findCommIdByGdeId(gdeIds);
			
			String gdeIdstr = "";
			for(Long sonId : gdeIdsList ){
				gdeIdstr = gdeIdstr+sonId+",";
			}
			if(gdeIdstr != ""){
				gdeIdstr=gdeIdstr.substring(0,gdeIdstr.length()-1);
			}
			
			resultList = commentsWebService.queryAddCommById(gdeIdstr, pageIndex,
					pageSize);
			
			setTotalCount(commentsWebService.findAddCommNumByGdeId(gdeIdstr));
			System.out.println("------"+commentsWebService.findAddCommNumByGdeId(gdeIdstr));
		}
		setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		resultMap.put("pageResult", page);
		return resultMap;
	}
	
	/**
	 * 查看某礼品的全部评论
	 * */
	/**全部评论分页*/
	@RequestMapping(value = "/findCommentByGdeId")
	@ResponseBody
	public Map<String, PageUtils<Comments>> findCommentByGdeId(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "ids", required = true) String gdeIds,
			HttpServletRequest request) {
		Map<String, PageUtils<Comments>> resultMap = new HashMap<String, PageUtils<Comments>>();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);
		setPageSize(pageSize);
		List<Comments> resultList = new ArrayList<Comments>();
		
		if (gdeIds == null) {
			throw new RuntimeException("礼品规格id有误");
		} else {
			
			List<Long> gdeIdsList = commentsWebService.findCommIdByGdeId(gdeIds);
			
			String gdeIdstr = "";
			for(Long sonId : gdeIdsList ){
				gdeIdstr = gdeIdstr+sonId+",";
			}
			if(gdeIdstr != ""){
				gdeIdstr=gdeIdstr.substring(0,gdeIdstr.length()-1);
			}
			
			resultList = commentsWebService.queryById(gdeIdstr, pageIndex,
					pageSize);
			setTotalCount(commentsWebService.findNumByGdeId(gdeIdstr));
		}
		setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		resultMap.put("pageResult", page);
		return resultMap;
	}
	
	/**
	 * 查看
	 */
	/*@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/list")
	public ModelAndView list(
			@RequestParam(value = "id", required = true) String gdeIds,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		if(gdeIds!=null){
		List resultList = new ArrayList();

		resultList = commentsWebService.queryById(gdeIds, getPageIndex(),
				getPageSize());
		setTotalCount(commentsWebService.totalCount());
		this.setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();
		view.addObject("gdeIds", gdeIds);
		view.addObject("pageResult", page);
		}else{
			throw new RuntimeException("礼品规格id有误");
		}
		return view;
		
	}*/
	
	
	/**
	 * 添加
	 * */
	
	@RequestMapping(value = "/saveWeb", method = RequestMethod.POST)
	public ModelAndView saveWeb(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		HttpSession session = request.getSession();
		String[] gdeId = request.getParameterValues("gdeId");//礼品id 
		String[] gdeName = request.getParameterValues("gdeName");//礼品名称 
		String[] content = request.getParameterValues("content");//评论内容 
		String[] contentDet = request.getParameterValues("contentDet");//礼品规格评论
		String[] logistics = request.getParameterValues("logistics");//物流评论
		String[] servicerComm = request.getParameterValues("servicerComm");//客服评论
		String[] anonymous = request.getParameterValues("anonymous");//是否公开评论
		String[] orderId = request.getParameterValues("orderId");//订单详情id 
		if(gdeId!=null && gdeId.length>0){
			for(int i=0;i<gdeId.length;i++){
				Comments comments = new Comments();
				//礼品规格
				GiftDetail giftDetail =  this.giftDetailService.getById(Long.parseLong(gdeId[i]));
				comments.setGiftDetail(giftDetail);
				comments.setGdeId(Long.parseLong(gdeId[i]));
				comments.setGdeName(gdeName[i]);
				comments.setContent(content[i]);
				comments.setContentDet(contentDet[i]);
				comments.setLogistics(logistics[i]);
				comments.setServicerComm(servicerComm[i]);
				comments.setAnonymous(Integer.parseInt(anonymous[i]));
				//会员
				HRTMember hrtMe = (HRTMember) session.getAttribute("HRTMember");
				comments.setHrtMember(hrtMe);
				comments.setHrtMemberId(hrtMe.getId());
				comments.setHrtMemberName(hrtMe.getMbrName());
				comments.setOrderId(Long.parseLong(orderId[i]));
				//是否。。。。
				comments.setIsAddComment(0);
				comments.setIsdelete(0);
				comments.setIsReply(0);
				commentsWebService.addComments(comments);
				mv.addObject(comments);
				mv.setViewName("redirect:/index.do");
			}
		}
		return mv;
	}
	
	
	
	/**
	 * 添加2
	 * */
	/*@RequestMapping(value = "/saveWeb", method = RequestMethod.POST)
	public void saveWeb(@ModelAttribute("comments") Comments comm,
			HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		System.out.println("aa==>"+comm.getGdeName());
		try {
			System.out.println("bb===>"+comm.getGdeId());
			System.out.println("cc===>"+comm.getOrderId());
			System.out.println("内容===>"+comm.getContent());
			HRTMember hrtMe = null;
			if(comm.getAnonymous()=='0'){
				hrtMe = (HRTMember) session.getAttribute("HRTMember");
				System.out.println("会员名称"+hrtMe.getMail());
				comm.setHrtMember(hrtMe);
				comm.setHrtMemberName(hrtMe.getMbrName());
			}
			comm.setIsAddComment(0);
			comm.setIsdelete(0);
			comm.setIsReply(0);
			//礼品规格
			GiftDetail giftDetail =  this.giftDetailService.getById(comm.getGdeId());
			comm.setGiftDetail(giftDetail);
			commentsWebService.addComments(comm);
			response.sendRedirect("comment_success.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
}
